package com.yu.waterstation.app.moudles.tasklog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.waterstation.app.moudles.tasklog.mapper.TaskLogMapper;
import com.yu.waterstation.app.moudles.tasklog.pojo.TaskLog;
import com.yu.waterstation.app.moudles.tasklog.pojo.dto.WorkerTaskStatistics;
import com.yu.waterstation.app.moudles.tasklog.service.TaskLogServiceImpl;
import com.yu.waterstation.common.annotation.GuestAccess;
import com.yu.waterstation.common.annotation.NeedToken;
import com.yu.waterstation.common.enums.TokenType;
import com.yu.waterstation.common.util.result.Result;
import com.yu.waterstation.common.util.session.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(tags = "送水日志")
@RestController
@RequestMapping("/taskLog")
public class TaskLogController {
    @Resource
    private TaskLogServiceImpl taskLogService;
    @Resource
    private TaskLogMapper taskLogMapper;

    @ApiOperation("某楼舍的送水记录 ")
    @GuestAccess
    @NeedToken(TokenType.ENUM_worker_token)
    @ApiImplicitParam(name = "token", paramType = "header")
    @GetMapping("/dorm")
    public Result<Page<TaskLog>> listOfDorm(
            @ApiParam("起始时间") @RequestParam long startTime,
            @ApiParam(example = "二区15舍") @RequestParam(required = false) String dorm,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize) {
        return Result.ok(taskLogService.getTaskLogsOfDorm(startTime, dorm, pageNo, pageSize));
    }

    @ApiOperation("寝室叫水历史记录 readOnly")
    @GuestAccess
    @GetMapping("/address/{addressId}")
    public Result<List<TaskLog>> listByAddressId(@PathVariable Integer addressId) {
        List<TaskLog> list = taskLogMapper.selectList(new QueryWrapper<TaskLog>()
                .eq("address_id", addressId).orderByDesc("timestamp"));
        return Result.ok(list);
    }

    @ApiOperation("撤销本次送水记录")
    @NeedToken(TokenType.ENUM_worker_token)
    @DeleteMapping("/rollback/{taskLogId}")
    public Result<Integer> rollbackTaskLog(@PathVariable Integer taskLogId, HttpServletRequest request) throws Exception {
        return taskLogService.rollbackTaskLog(taskLogId, SessionUtil.getWorkerIdFromRequest(request));
    }

    @ApiOperation("员工送水记录统计")
    @GuestAccess
    @NeedToken(TokenType.ENUM_worker_token)
    @GetMapping("/statistics")
    public Result<WorkerTaskStatistics> getWorkerTaskTotal(HttpServletRequest request) throws Exception {
        WorkerTaskStatistics obj = taskLogMapper.getWorkerTaskStatistics(SessionUtil.getWorkerIdFromRequest(request));
        if (obj == null)
            obj = new WorkerTaskStatistics();
        return Result.ok(obj);
    }


}
